<?


require_once  $_SERVER['DOCUMENT_ROOT']."/../_lib/base/common.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.statistics.php";


// user authentication
user_auth();

// HEAD //
require_once SITE_ROOT_PATH."/_lib/html/control_head.php";



// FUNCTION // Display home ======================
function home(){
?>


<h2>Statistics</h2>


<h3>Page Hit Reports</h3>


<p>To view the page hit reports</p>

<form method='GET' action='<?= $_SERVER['PHP_SELF']?>'>
	
	<input type="hidden" name="action" value="hit_report">

	<label>Start Date</label>&nbsp;&nbsp;
	<? 	date_input("start_date",date('Y-m-d') ); ?>
	
	<br>
	
	<label>End Date</label>&nbsp;&nbsp;
	<? 	date_input("end_date",date('Y-m-d') ); ?>

	<br>

	<input name='Submit' value='Submit' type='submit'>
	
</form>



<h3>References Report</h3>


<p>To view the number of references made and lookups</p>

<form method='GET' action='<?= $_SERVER['PHP_SELF']?>'>
	
	<input type="hidden" name="action" value="reference_report">

	<label>Start Date</label>&nbsp;&nbsp;
	<? 	date_input("start_date",date('Y-m-d') ); ?>
	
	<br>
	
	<label>End Date</label>&nbsp;&nbsp;
	<? 	date_input("end_date",date('Y-m-d') ); ?>

	<br>

	<input name='Submit' value='Submit' type='submit'>
	
</form>





<?
}
// END FUNCTION ================================




// FUNCTION // Display reports ======================
function dealer_report(){

$stats = new stats();
$users = new users();
$month = $_REQUEST['dates_year']."-".$_REQUEST['dates_month'];


?>

<h2>View reports</h2>


<h3>Search date: <?= $month; ?></h3>

<table class="libra">

<tr bgcolor="#eeeeee">
	<th>ID</th>
	<th>Dealer</th>
	<th>Search Results</th>
	<th>Details Page Impressions</th>
	<th>Website Referals</th>
</tr>
<?

	//$details = $stats->get_report($month);
	$details = $users->get_all_users('0');
	
	$xls = $xls."ID,Dealer,Number of search results,Number page impression,Number of referals\n";	
	$i = 0;

	foreach($details as $value) {
	
		// highlight rows
		if($i % 2){
			$highlight = "style='background:#ececec'";
		}else{
			$highlight = "";
		}
	
?>
<tr <?= $highlight ?>>
	<td align="left"><?= $value ['user_id'] ?></td>
	<td align="left"><?= $value['dealer_name'] ?></td>
	<td align="center"><?= $stats->get_stats_results($month,$value['user_id']); ?></td>
	<td align="center"><?= $stats->get_stats_details($month,$value['user_id']); ?></td>
	<td align="center"><?= $stats->get_stats_referals($month,$value['user_id']); ?></td>
</tr>
<?

	/*
$xls .= $value['user_id'].",".$value['dealer_name'].",".$value['rcounter'].",".$value['dcounter'].",".$value['ucounter']."\n";


	srand(time());
	$rval = 1000 + (rand() % 10000);
	$strFile = "stats".$rval.".csv";
	
	if(file_exists($strFile)) unlink($strFile);
	
	if($file = fopen($strFile,"w") OR die()){
		fputs($file,$xls);
	}else{
		$strFile = "";
	}

	*/


$i++;

}
?>
</table>


<?


}
// END FUNCTION ================================





// FUNCTION // Display report ======================
function hit_report(){

	
	
	$start_date = mktime(0, 0, 0, $_REQUEST['start_date_month'], $_REQUEST['start_date_day'], $_REQUEST['start_date_year']);
	$end_date = mktime(0, 0, 0, $_REQUEST['end_date_month'], $_REQUEST['end_date_day'], $_REQUEST['end_date_year']);

	$stats = new stats();
	$details = $stats->get_stats_hits_details($start_date, $end_date);

?>

<h2>View reports</h2>


<h3>Search dates <?= date('j M y',$start_date)?> - <?= date('j M y',$end_date)?></h3>

<table class="libra">

<tr bgcolor="#eeeeee">
	<th width="400">Page URL</th>
	<th>Total Hits</th>
</tr>
<?
	foreach($details as $value) {
	
		if($i % 2){
			$highlight = "style='background:#ececec'";
		}else{
			$highlight = "";
		}
	
?>
<tr <?= $highlight ?>>
	<td><?= $value ['page'] ?></td>
	<td align="center"><?= $value['hits'] ?></td>
</tr>
<?

$i++;

}
?>
</table>


<?


}
// END FUNCTION ================================






// FUNCTION // Display report ======================
function reference_report(){

	
	
	$start_date = mktime(0, 0, 0, $_REQUEST['start_date_month'], $_REQUEST['start_date_day'], $_REQUEST['start_date_year']);
	$end_date = mktime(0, 0, 0, $_REQUEST['end_date_month'], $_REQUEST['end_date_day'], $_REQUEST['end_date_year']);

	$stats = new stats();
	$values = $stats->get_stats_reference($start_date, $end_date);

?>

<h2>View reports</h2>


<h3>Search dates <?= date('j M y',$start_date)?> - <?= date('j M y',$end_date)?></h3>

<table class="libra">

<tr bgcolor="#eeeeee">
	<th width="200">Date</th>
	<th width="150">Reference count</th>
	<th width="150">Lookup count</th>
</tr>
<?
	foreach($values as $value) {
	
		if($i % 2){
			$highlight = "style='background:#ececec'";
		}else{
			$highlight = "";
		}
	
?>
<tr <?= $highlight ?>>
	<td><?= $value['nicest_ref_date'] ?></td>
	<td align="center"><?= $value['reference_count'] ?></td>
	<td align="center"><?= $stats->get_stats_reference_lookups($value['nice_ref_date']) ?></td>
</tr>
<?

$i++;

}
?>
</table>


<?


}
// END FUNCTION ================================





switch($_REQUEST['action']){
	default;
		home();
	break;
	case "reference_report";
		reference_report();
	break;
	case "hit_report";
		hit_report();
	break;
}

	
require_once SITE_ROOT_PATH."/_lib/html/control_foot.php";
	
?>